﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Content.Master" Inherits="System.Web.Mvc.ViewPage<MethodWorx.CMS.UI.Models.ConfigurePropertiesModel>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <script language="javascript">
        $(document).ready(function () {
            $("#properties").accordion({ active: <%=this.ViewData.Model.SelectedPropertyIndex %>, height: "auto" });
            $("#properties").bind("accordionchange", function (event, ui) {
                $("#SelectedPropertyIndex").val(ui.newContent.attr("index"));
            });
            
            $(".propertyType").change(function () {
                $("#refresh").click();

            });

            $(window).resize(function()
            {
                resize();
            });

            var buttons = new Array();
            
              <%if(this.Model.Templates != null) { %>
              buttons.push({ text: 'Template', type: 'label' });
            buttons.push({ text: 'Template', id: "templates", change: function() { selectTemplate() }, options: 
            [
            <%foreach(var t in this.Model.Templates) {%>
                { value: "<%=t.Id %>", text: "<%=t.Name %>"}<%=(t != this.Model.Templates.Last()) ? "," : "" %>
            <%} %>
            ]});
            <%} %>

            <%if(this.Model.Properties.Any()) { %>
            buttons.push({ text: "Save Properties", hasText: false, id: "save", icon: "<%=Url.Content("~/images/icons/disk.png")%>" });
            buttons.push({ text: "Refresh Properties", hasText: false, id: "refresh", icon: "<%=Url.Content("~/images/icons/arrow_refresh.png")%>" });
            <%} %>
            buttons.push({ text: "Add Property", hasText: false, id: "add", icon: "<%=Url.Content("~/images/icons/note_add.png")%>" });
            <%if(this.ViewData["pageId"] != null || this.ViewData["contentItemId"] != null) { %>
                buttons.push({ text: 'Edit Page Properties Values', hasText: false, id: 'editPageProperties', icon: '<%=Url.Content("~/images/icons/table_edit.png")%>' });
            <%} %>

          

            MethodWorx.CMS.Toolbar.setButtons(buttons, "#toolbar");

            $("#add").click(function () { $("#addButton").click(); });
            <%if(this.Model.Properties.Any()) { %>
            $("#save").click(function () { $("#saveButton").click(); });
            $("#refresh").click(function () { $("#refreshButton").click(); });
            <%} %>
            
            <%if(this.ViewData["pageId"] != null || this.ViewData["contentItemId"] != null) { %>
              $("#editPageProperties").click(function () {
                <%if(this.ViewData.Model.PageTemplate != null) { %>
                window.location.href = "<%=Url.Content("~/Page/Properties")%>/<%=this.ViewData["pageId"]%>";
                <%}else { %>
                window.location.href = "<%=Url.Content("~/Content/EditContentItem")%>/<%=this.ViewData["contentItemId"]%>?pageId=<%=this.ViewData["pageId"]%>";
                <%} %>
            });
            <%} %>

            resize();
            <%if(this.Model.Templates != null) { %>
            $("#templates").val('<%=this.ViewData.Model.PageTemplate.Id %>');
            <%} %>
        });

         
        function selectTemplate() {
                window.location.href = $("#templates").val() + "?pageId=<%=this.ViewData["pageId"] %>";
        }
       
        
        function resize()
        {
            $("#container").height($(window).height() - 37);
        }
    </script>

    <%using(var form = Html.BeginForm()) {%>
        <%=Html.Hidden("Id")%>
        <%=Html.Hidden("SelectedPropertyIndex") %>
        <%=Html.Hidden("pageId") %>
        <%=Html.Hidden("contentItemId") %>
        <button name="submit" value="add" id="addButton" style="display:none"></button>
        <button name="submit" value="save" id="saveButton" style="display:none"></button>
        <button name="submit" value="refresh" id="refreshButton" style="display:none"></button>
        <h3>
        <%if(this.ViewData.Model.ContentTemplate != null) { %>
        <%:this.ViewData.Model.ContentTemplate.Name %> Template Properties
        <%} else { %>
        <%:this.ViewData.Model.PageTemplate.Name %> Template Properties
          <%} %>
          </h3>
        <%if(!this.Model.Properties.Any()) { %>
        <div style="text-align:center">
            <div style="text-align:center">
            <p>No properties exist for this template - to add a property click <img src="<%=Url.Content("~/images/icons/note_add.png")%>" align="absmiddle"/></p>
        </div>
        </div>
        <% } else { %>
            <div style="text-align:left;">
                <p>Below are the following property configuration for the template, to set the page property values click <img src="<%=Url.Content("~/images/icons/table_edit.png")%>" align="absmiddle"/></p>
            </div>
            <div id="properties">
                <%
                    foreach(var property in this.Model.Properties) { %>
                    <h3>
                        <a><%=Html.TextBox("PropertyDetails[" + property.Index + "].Name", property.Name) %> - <%=Html.DropDownList("PropertyDetails[" + property.Index + "].Type", this.Model.GetPropertyTypeSelectList(property), new { @class = "propertyType" })%>
                        <%=Html.ValidationMessage("PropertyDetails[" + property.Index + "].Name") %>
                        </a>
                    </h3>
                    <div index="<%=property.Index %>" >
                        <%=Html.Hidden("PropertyDetails[" + property.Index + "].Index", property.Index) %>
                        <%=Html.Hidden("PropertyDetails[" + property.Index + "].Type", property.Type) %>
                        <%=Html.FormCheckBox("Mandatory", "PropertyDetails[" + property.Index + "].Mandatory", property.Mandatory) %>
                        <%
                            var pc = new MethodWorx.CMS.Configuration.PropertyConfiguration();
                            object temp = null;
                            MethodWorx.CMS.AssemblyResourceReference arr = null;
                            //
                            //  from the definition of the type and configuration
                            //  load the properties and the editor ascx
                            foreach (var p in pc.GetPropertyConfigurations())
                            {
                                if(p.Name.ToLower() == property.Type.ToLower())
                                {
                                    temp = Activator.CreateInstance(typeof(MethodWorx.CMS.Plugins.PropertyDetails<>).MakeGenericType(p.GetDetailsType()));
                                    arr = p.GetPropertyEditorControl();

                                    temp.GetType().InvokeMember("Index", System.Reflection.BindingFlags.SetProperty, null, temp, new object[] { property.Index });
                                    temp.GetType().InvokeMember("Name", System.Reflection.BindingFlags.SetProperty, null, temp, new object[] { property.Name });
                                    temp.GetType().InvokeMember("Mandatory", System.Reflection.BindingFlags.SetProperty, null, temp, new object[] { property.Mandatory });
                                    temp.GetType().InvokeMember("Type", System.Reflection.BindingFlags.SetProperty, null, temp, new object[] { property.Type });
                                }
                            }
                            if (property.Custom != null)
                            {
                                temp.GetType().InvokeMember("Custom", System.Reflection.BindingFlags.SetProperty, null, temp, new object[] { property.Custom });
                            }
                        %>
                        <%if(temp != null) Html.RenderPartial(arr.Name, temp); %>
                        <div class="formItem">
                        <a href="?submit=remove&index=<%=property.Index%>">remove</a>
                        </div>
                    </div>
                <%} %>
            </div>
       <%} %>
    <%}%>

</asp:Content>
